export default {
	/**
	 * 下载图片打破本地
	 * @param {String} url 图片地址
	 */
	async saveImageToPhoto(url) {
		console.log('图片地址', url);

		let pic = await uni.getImageInfo({
			src: url
		})
		console.log('下载图片', pic);

		let auth = await this.hasAuthSetting()
		console.log('检测授权', auth);

		let save = await uni.saveImageToPhotosAlbum({
			filePath: pic.path
		})
		console.log('保存图片', save);
	},
	/**
	 * 检测授权
	 */
	hasAuthSetting() {
		return new Promise((resolve, reject) => {
			uni.getSetting({
				success(res) {
					if (res.authSetting['scope.writePhotosAlbum']) {
						resolve('ok')
					} else if (res.authSetting['scope.writePhotosAlbum'] === false) {
						uni.openSetting({
							success(r) {
								if (r.authSetting['scope.writePhotosAlbum']) {
									resolve('ok')
								}
							}
						});
					} else {
						uni.authorize({
							scope: 'scope.writePhotosAlbum',
							success() {
								resolve('ok')
							}
						})
					}
				}
			});
		});
	}
}